<?php 
namespace Home\Controller;
use Think\Controller;

/**
* 用户中心控制器
*/
class UsercoreController extends Controller
{

	public function out(){
				if (isset($_SESSION['uid'])) {
			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}
	public function index()
	{	
		if (isset($_SESSION['uid'])) {
			$uid = $_SESSION['uid'];
			$sql = "select face,city,create_time,kiss,sex,nikename from user where id = {$uid}";
			$res = current(M()->query($sql));
			$this->assign('res',$res);

			//分配游客
			$sql ="select t1.face,t1.nikename,t2.create_time,t2.uid from user t1 inner join view t2 on t1.id = t2.uid where t2.main_uid ='{$uid}'";
			$view =M()->query($sql);
			$this->assign('view',$view);
			//分配游客

			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}
	public function set()
	{	


		if (isset($_SESSION['uid'])) {

			$sql ="select * from user where id=".$_SESSION['uid'];
			$res =M()->query($sql);
			$this->assign('res',$res);
			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}

	public function message()
	{

		if (isset($_SESSION['uid'])) {
			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}

	public function face()
	{
		$this->out();
		// if (isset($_SESSION['uid'])) {
		// 	$nikename = $_SESSION['nikename'];
		// 	$this->assign('nikename',$nikename);
		// 	$this->display();
		// }else{
		// 	$this->success('请重新登录！',U('home/index/index'));
		// }
	}

	public function bind()
	{
		if (isset($_SESSION['uid'])) {
			$sql ="select * from user where id=".$_SESSION['uid'];
			$res =M()->query($sql);
			$this->assign('res',$res);
			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}

	public function activate()
	{
		if (isset($_SESSION['uid'])) {
			$this->display();
		}else{
			$this->success('请重新登录！',U('home/index/index'));
		}
	}

	public function pass()
	{
		$this->out();
	}
	public function u()
	{
		
			$id = I('get.id'); 
			$sql = "select * from user where id = $id";
			$res = M()->query($sql);
			$this->assign('res',$res);

			$sql ="select * from question where uid = $id order by create_time desc";
			$que = M()->query($sql);
			$this->assign('que',$que);

			$sql ="select t1.title,t1.id,t2.create_time,t2.content from question t1 inner join answer t2 on t1.id = t2.qid where t1.uid = $id ";
			$ans = M()->query($sql);
			$this->assign('ans',$ans);

			//记录访问个人主页的游客数据
			if(isset($_SESSION['uid'])){
				if($id!=$_SESSION['uid']){
					$uid = $_SESSION['uid'];
					$sql ="select * from view where main_uid = '{$id}' and uid = '{$uid}'";
					$res =M()->query($sql);
					if(!$res){
						$data = array();
						$data['main_uid'] = $id;
						$data['uid'] = $_SESSION['uid'];
						$data['create_time'] = time();
						$add =M('view')->add($data);	
					}else{
						$time = time();
						$main_uid = $id;
						$uid = $_SESSION['uid']; 
						$sql = "update view set create_time = '{$time}' where main_uid ='{$main_uid}' and uid = '{$uid}' ";
						$update = M()->execute($sql);
					}

				}
			}
			//记录访问个人主页的游客数据

			

			$this->display();
	
			
	}

	public function email()
	{
		
		$email =I('post.email','');
       $preg = "/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/";
       $c = preg_match($preg,$email);
	       if($c==0){
	            $arr = array(
	            	'error'=>0,
	            	'info' =>"邮箱格式错误！", 
	            	);
	            echo json_encode($arr);
	            exit;
	    		}
		$sql ="select * from user where email='{$email}'";
		$res =M()->query($sql);
			if ($res) {
				if ($res[0]['id']!=$_SESSION['uid']) {
					$arr = array(
					'error'=>0,
					'info'=>"该邮箱以存在，请重新填写！",	
					);
					echo json_encode($arr);
				}else{
					$arr = array(
					'error'=>1,
					'info'=>"邮箱未修改"	
					);
					echo json_encode($arr);
				}	
			}else{
				$arr = array(
					'error'=>1,
					'info'=>"该邮箱可以使用！",	
					);
				echo json_encode($arr);
			}

	}

	public function nike()
	{
		$nikename =I('post.nikename','');
    	$preg='/^[a-zA-Z0-9\x{4e00}-\x{9fa5}\_&\*\.\^@%*?]{2,7}$/u';
    	$c= preg_match($preg, $nikename);
	    	if($c==0){
	            $arr = array(
	            	'error'=>0,
	            	'info' =>"该昵称不合法，长度为2-7位，请重新写", 
	            	);
	            echo json_encode($arr);
	            exit;
	    		}
		$sql ="select * from user where nikename='{$nikename}'";
		$res =M()->query($sql);
			if ($res) {
				if($res[0]['id']!=$_SESSION['uid']){
					$arr = array(
					'error'=>0,
					'info'=>"该昵称以存在，请重新填写！",	
					);
					echo json_encode($arr);
				}else{
					$arr = array(
					'error'=>1,
					'info'=>"昵称未修改！",	
					);
					echo json_encode($arr);
				}
				
			}else{
				$arr = array(
					'error'=>1,
					'info'=>"该昵称可以使用！",	
					);
				echo json_encode($arr);
			}
	}

	public function setdata()
	{	
		$id = $_SESSION['uid'];
		$data =I('post.');
		$nikename =$data['nikename'];
		$city =$data['city'];
		$sign =$data['sign'];
		$email =$data['email'];
		$sql = "update user set nikename ='{$nikename}',city ='{$city}',resume ='{$sign}',email ='{$email}' where id = $id";
		$res =M()->execute($sql);
		if ($res) {
			$arr = array('error' =>1 ,'info'=>'修改成功' );
			echo json_encode($arr);
		}else{
			$arr = array('error' =>0,'info'=>'修改成功' );
			echo json_encode($arr);
		}
	}

	public function setpass()
	{
		$data = I('post.');
		$pass = md5($data['repass']);
		$id = $_SESSION['uid'];
		$sql  = "update user set password ='{$pass}' where id=$id ";
		$res = M()->execute($sql);
		if ($res) {
			$arr = array('error' =>1 ,'info'=>'修改成功' );
			echo json_encode($arr);
		}else{
			$arr = array('info'=>'不能跟上次密码相同，请重新输入！' );
			echo json_encode($arr);
		}

	}

	public function upload()
	{

		    $upload = new \Think\Upload();
		    $upload->maxSize   =      3145728;
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
		    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		    $info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
				echo  $upload->getError();
		 	}else{// 上传成功
				 $path = "Uploads/".$info['face']['savepath'].$info['face']['savename'];
       	 		$sql = "update user set face = '{$path}' where id = '{$_SESSION['uid']}'";
	        	$res = M()->execute($sql);

	        	if($res){
	        	$_SESSION['face'] = $path;
		        	$arr = array(
		        		'error'=>0,
		        		'info'=>'上传成功',
		        		'face'=>$path
		        		);
		        	echo json_encode($arr);
		        }else{
		        	$arr = array(
		        		'error'=>1,
		        		'info'=>'上传失败'
		        		);
		  	      	echo json_encode($arr);
	        	}
   		}
	}
	public function check()
	{	
		$uid = $_SESSION['uid'];
		$sql = "select * from user where id = '$uid'";
		var_dump($sql);
		$res =M()->query($sql);
		if (empty($res[0]['email']) && empty($res[0]['password'])) {
			$id = $res[0]['openid'];
			$sql = "update user set openid = '' where id=".$uid;
			$res =M()->execute($sql);
			if ($res) {
				$this->success('已解除绑定',U('home/usercore/bind'));
			}

			
		}else{
			echo "不解除";
		}
	}

	public function delbing()
	{
		$this->check();
	}


















}


 ?>